package com.resource.gsLoader
{
	import com.greensock.events.LoaderEvent;
	import com.greensock.loading.DataLoader;
	import com.greensock.loading.ImageLoader;
	import com.greensock.loading.LoaderMax;
	import com.greensock.loading.core.LoaderCore;
	import com.greensock.loading.core.LoaderItem;
	
	import flash.display.DisplayObject;
	import flash.events.EventDispatcher;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.system.SecurityDomain;
	import flash.utils.Dictionary;
	
	import org.osmf.events.LoadEvent;
	
	/**
	 * 自定义loader类
	 * @author xiezhicheng
	 */
	public final class MyImageLoader extends LoaderBase
	{
		private static var _instance:MyImageLoader;
	
		/**是否有重复资源*/
		private var _repeat:Boolean = false;
		public function MyImageLoader()
		{
			super();
			if (_instance == null) 
			{
				_instance = this;
				init();
				
			}else{
				throw new Error("MyImageLoader is a single class");
			}
			
		}
		
		/**
		 * 创建实例
		 * @return 
		 */
		public static function getInstance():MyImageLoader
		{
			if (_instance == null) 
			{
				_instance = new MyImageLoader();
			}	
			return _instance;
			
		}
		
		
		override protected function init():void
		{	
			_loadingArr = [];
		}
		
		
		/**
		 * 添加Imageloader
		 * @param arr
		 */
		override public function loadMultiple(arr:Array):void
		{
			var num:int = 0;
			_repeat = false;
			while(arr[num])
			{
				var loader:ImageLoader = new ImageLoader(arr[num][0]);
				
				//如果当前请求资源已被加载,则直接把该引用通过事件派发出去
				if (_loadDic[loader.url] != null) 
				{
					if (_resourceDic[loader.url] == null) //过滤相同资源的重复请求
					{
						_repeat = true;	
						_resourceDic[loader.url] = loader;
					}
				}else
				{
					loader.addEventListener(LoaderEvent.COMPLETE,imageLoadCompete);	
				}
				
				
				if (_loadingArr.indexOf(loader.url) == -1 && loader.url != "" && !_repeat) 
				{
					_callBackDic[loader.url] = [arr[num][1]];
					_loadingArr.push(loader.url);
					_loader.append(loader);
				}	
				else{
					_callBackDic[loader.url].push(arr[num][1]);
				}
				
				num++;
			}
			
			if(_repeat){//若有重复资源,则将过滤好的资源派发完成事件
				for each (var i:ImageLoader in _resourceDic) 
				{
					var e:LoaderEvent = new LoaderEvent(LoaderEvent.COMPLETE,i);	
					i.addEventListener(LoaderEvent.COMPLETE,$imageLoadCompete);
					i.dispatchEvent(e);
				}		
			}
			_loader.autoDispose = true;
			_loader.load();
			
		}
		
		
		private function imageLoadCompete(e:LoaderEvent):void
		{
			e.target.removeEventListener(LoaderEvent.COMPLETE,imageLoadCompete);		
			
			
			//将已加载完的资源保存方便下次调用
			_loadDic[e.target.url] = e.target.content;
			//加载完成时将该url从正在加载的数组中移除
			_loadingArr.splice(_loadingArr.indexOf(e.target.url),1);
			
			if (_callBackDic[e.target.url] != null) 
			{
				for each(var i:Function in _callBackDic[e.target.url])
				{
					if(i != null){
						i(e.target.content);
						//(_callBackDic[e.target.url] as Array).shift();
					}
					
				}
				
				_callBackDic[e.target.url].length = 0;
				
			}
		}
		
		
		/**
		 * 当资源重复时,直接调用其回调函数
		 * */
		private function $imageLoadCompete(e:LoaderEvent):void
		{
			e.target.removeEventListener(LoaderEvent.COMPLETE,$imageLoadCompete);
			
			delete _resourceDic[e.target.url];
			
			if (_callBackDic[e.target.url] != null) 
			{
				for each(var i:Function in _callBackDic[e.target.url])
				{
					if(i != null){
						i(_loadDic[e.target.url] as DisplayObject);
					}
					
				}
				
				_callBackDic[e.target.url].length = 0;
			}
		}	
	}
}